//
//  MCShareTicketViewController.swift
//  StoreManage
//
//  Created by MC on 2018/1/31.
//  Copyright © 2018年 MC. All rights reserved.
//

import UIKit

class MCShareStoreViewController: MCViewController {
    
    var storeIconImageStr = ""
    var storeName = ""
    var storeId = 0
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        baseSetting()
        
        initUI()
        
        sendNetworking()
    }
    
    // MARK: - System Delegate
    
    // MARK: - Custom Delegate
    
    // MARK: - Action
    @objc func rightItemClicked() {
        view.window?.addSubview(shareWayView)
        shareWayView.snp.remakeConstraints { (make) ->Void in
            make.edges.equalTo(view.window!)
        }
    }

    
    // MARK: - Network
    
    func sendNetworking() {
        
        storeIconImageView.sd_setImage(with: URL.init(string: storeIconImageStr), placeholderImage: #imageLiteral(resourceName: "Store_bg"))
        storeNameLabel.text = storeName
        
        
        MCNetwork_post(url: JAVA_getShareSmallProgramsCode, para: [:] as! [String : AnyObject], vc: self, isHiddenTabbar: true, success: { (response) in
            
            let dataDict = response["data"] as? [String:Any] ?? [:]
            let urlStr = dataDict["imgUrl"] as? String ?? ""
            
            self.qrImageView.sd_setImage(with: URL.init(string: urlStr), placeholderImage: #imageLiteral(resourceName: "Reuse_placeholder_1_1"))
            
        }, dislike: { (response) in }) { (error) in }
    }
    
    // MARK: - Method
    func baseSetting() {
        self.title = "分享店铺"
        self.view.backgroundColor = UIColor.white
        
        navigationItem.rightBarButtonItem = MCSetBarButtonItem_image(#imageLiteral(resourceName: "Reuse_share_black"), target: self, selector: #selector(rightItemClicked))
    }
    
    func initUI() {
        
        let bg_W = MCScreenWidth - 50
        let topImageH = bg_W / 2
        
        
        view.addSubview(bgView)
        bgView.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(25)
            make.width.equalTo(bg_W)
            make.top.equalTo(50)
            make.height.equalTo(topImageH + 260)
        }
        
        
        bgView.addSubview(storeImageView)
        storeImageView.snp.remakeConstraints { (make) -> Void in
            make.left.right.top.equalTo(bgView)
            make.height.equalTo(topImageH)
        }
        
        storeImageView.addSubview(titleImageView)
        titleImageView.snp.remakeConstraints { (make) -> Void in
            make.centerX.equalTo(storeImageView)
            make.top.equalTo(55)
        }
        
        
        
        bgView.addSubview(storeIconBgView)
        storeIconBgView.snp.remakeConstraints { (make) -> Void in
            make.centerX.equalTo(storeImageView)
            make.width.height.equalTo(81)
            make.centerY.equalTo(storeImageView.snp.bottom)
        }
        

        storeIconBgView.addSubview(storeIconImageView)
        storeIconImageView.snp.remakeConstraints { (make) -> Void in
            make.edges.equalTo(storeIconBgView).inset(UIEdgeInsetsMake(1, 1, 1, 1))
        }

        
        bgView.addSubview(storeNameLabel)
        storeNameLabel.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(10)
            make.right.equalTo(-10)
            make.height.equalTo(15)
            make.top.equalTo(storeImageView.snp.bottom).offset(50)
        }
        
        
        bgView.addSubview(qrImageView)
        qrImageView.snp.remakeConstraints { (make) ->Void in
            make.top.equalTo(storeNameLabel).offset(40)
            make.centerX.equalTo(bgView)
            make.width.height.equalTo(100)
        }
        
        bgView.addSubview(promptLabel)
        promptLabel.snp.remakeConstraints { (make) ->Void in
            make.centerX.equalTo(bgView)
            make.top.equalTo(qrImageView.snp.bottom).offset(15)
        }
        
    }
    
    
    // MARK: - Setter & Getter
    
    
    lazy var bgView: MCShareBaseBackgroundView = {
        let view = MCShareBaseBackgroundView()
        return view
    }()
    
    lazy var shareWayView: MCShareBaseShareWayView = {
        let view = MCShareBaseShareWayView()
        view.superView = self.view
        view.saveView = self.bgView

        

        
        return view
    }()
    

    
    lazy var storeNameLabel: UILabel = {
        let label = UILabel()
        label.font = MCFont(15)
        label.textColor = UIColor.black
        label.textAlignment = NSTextAlignment.center
        return label
    }()
    
    private lazy var storeImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.backgroundColor = UIColor.white
        imageView.image = #imageLiteral(resourceName: "Share_store_top")
        imageView.layer.cornerRadius = 7
        imageView.layer.masksToBounds = true
        return imageView
    }()
    
    private lazy var titleImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.image = #imageLiteral(resourceName: "Share_storeName")
        return imageView
    }()

    private lazy var storeIconBgView: UIView = {
        let imageView = UIView()
        imageView.backgroundColor = UIColor.white
        imageView.layer.borderColor = UIColor.MCGray_light.cgColor
        imageView.layer.borderWidth = 1
        imageView.layer.masksToBounds = true
        imageView.layer.cornerRadius = 40.5
        return imageView
    }()
    
    private lazy var storeIconImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.backgroundColor = UIColor.white
        imageView.layer.borderColor = UIColor.white.cgColor
        imageView.layer.borderWidth = 3
        imageView.layer.masksToBounds = true
        imageView.layer.cornerRadius = 40
        return imageView
    }()
    
    private lazy var qrImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.layer.cornerRadius = 5
        imageView.layer.masksToBounds = true
        imageView.backgroundColor = UIColor.white
        return imageView
    }()

    private lazy var promptLabel: UILabel = {
        let label = UILabel()
        label.font = MCFont(12)
        label.textColor = UIColor.MCGray
        label.numberOfLines = 0
        label.textAlignment = NSTextAlignment.center
        label.text =  "扫描识别二维码进入\n店铺小程序"
        return label
    }()

}

